﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml.Media.Imaging;
using MusicExplorer.Tools;
using Windows.UI.Core;
using Windows.Graphics.Imaging;
using Windows.Storage.Streams;

namespace MusicExplorer.Extensions
{
    public static class BitmapImageExtensions
    {
        private static Dictionary<Guid, BitmapImage> s_images = new Dictionary<Guid, BitmapImage>();
        public static async Task FillWithImageRepositoryAsync(this BitmapImage image, Model.Repository repository)
        {
            if(!s_images.ContainsKey(repository.Id))
            {
                using(var stream = await repository.Image.GetStreamAsync())
                {
                    await image.SetSourceAsync(stream);
                    //s_images[repository.Id] = image;
                }
            }
            //image = s_images[repository.Id];
        }

        private static Dictionary<Guid, BitmapImage> s_icons = new Dictionary<Guid, BitmapImage>();
        public static async Task FillWithIconRepositoryAsync(this BitmapImage icon, Model.Repository repository)
        {
            if (!s_icons.ContainsKey(repository.Id))
            {
                using (var stream = await repository.Icon.GetStreamAsync())
                {
                    await icon.SetSourceAsync(stream);
                    //s_icons[repository.Id] = icon;
                }
            }
            //icon = s_icons[repository.Id];
        }

        public static async Task FillWithBytes(this BitmapImage image, byte[] byteArray)
        {
            if (byteArray != null)
            {
                using (var stream = await byteArray.GetStreamAsync())
                {
                    await image.SetSourceAsync(stream);
                }
            }
        }
    }
}
